/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;

#endregion

namespace DotNetNuke.ModuleToolkit.Common
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// A Static class containing utility methods
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// <history>
    ///     [anurse]    05/04/2006  Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public static class ControlUtils
    {
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Searches for the specified control, using the specified reference point
        /// </summary>
        /// <param name="controlID">The control to find</param>
        /// <param name="referencePoint">The reference point for the search</param>
        /// <returns>The control with the specified id, or null if it could not be found</returns>
        /// <history>
        /// 	[anurse]	05/04/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static Control FindControl(Control referencePoint, string controlID)
        {
            // Is the control the top-level page?
            if (referencePoint.Page == referencePoint)
                return referencePoint.FindControl(controlID);

            // Move up the tree of NamingContainers
            Control currentPoint = referencePoint;
            Control foundControl = null;
            while ((foundControl == null) && (currentPoint != currentPoint.Page))
            {
                currentPoint = currentPoint.NamingContainer;

                // If there's no NamingContainer, we can't continue the search
                if (currentPoint == null)
                    return null;
                foundControl = currentPoint.FindControl(controlID);
            }
            return foundControl;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Searches for the first parent of this control of the specified type
        /// </summary>
        /// <typeparam name="T">The type of control to find</typeparam>
        /// <param name="referencePoint">The reference point for the search</param>
        /// <returns>The parent of the specified type, or null if it could not be found</returns>
        /// <history>
        /// 	[anurse]	05/04/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static T FindParent<T>(Control referencePoint) where T : class
        {
            if (referencePoint.Parent == referencePoint)
                return null;

            Control currentPoint = referencePoint;
            while (currentPoint != currentPoint.Parent)
            {
                currentPoint = currentPoint.Parent;
                if (currentPoint == null)
                    return null;

                // This is the way to do this according to the Microsoft Code Analysis Tool
                T convCurrentPoint = currentPoint as T;
                if (convCurrentPoint != null)
                    return convCurrentPoint;
            }
            return null;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Searches for the first child control (recursively) of this control of the specified type
        /// </summary>
        /// <typeparam name="T">The type of control to find</typeparam>
        /// <param name="collection">The controls collection to search</param>
        /// <returns>The parent of the specified type, or null if it could not be found</returns>
        /// <history>
        /// 	[anurse]	08/18/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static T FindChild<T>(Control referencePoint) where T : class
        {
            return FindChild<T>(referencePoint.Controls);
        }
        
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Searches for the first item/child of an item (recursively) in the specified controls collection
        /// of the specified type
        /// </summary>
        /// <typeparam name="T">The type of control to find</typeparam>
        /// <param name="collection">The controls collection to search</param>
        /// <returns>The parent of the specified type, or null if it could not be found</returns>
        /// <history>
        /// 	[anurse]	08/18/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static T FindChild<T>(ControlCollection collection) where T : class
        {
            foreach (Control control in collection)
            {
                // This is the way to do this according to the Microsoft Code Analysis Tool
                T convControl = control as T;
                if (convControl != null)
                    return convControl;

                if (control.HasControls())
                    convControl = FindChild<T>(control.Controls) as T;
                if (convControl != null)
                    return convControl;
            }
            return null;
        }
    }
}
